package com.solocredit.finance.ui.loan.impl

import android.content.Context
import com.solocredit.finance.http.ApiService
import com.solocredit.finance.http.ApiService.addCommonParams
import com.solocredit.finance.http.ApiService.addRandomParams
import com.solocredit.finance.http.HttpHelper
import com.solocredit.finance.http.IHttpCallback
import com.solocredit.finance.mvp.presenter.MvpPresenter
import com.solocredit.finance.ui.loan.view.IMullaLoanPaymentStatusView
import com.solocredit.finance.utils.helper.SharedHelper
import org.json.JSONObject

/**
 * MullaFlow
 * @Author wwdeng
 * @CreateTime: 2024/8/7 13:45
 * @Annotation：支付状态查询 Presenter 实现
 */
class MullaLoanPaymentStatusPresenterImpl(context: Context?) : MvpPresenter<IMullaLoanPaymentStatusView>(context) {

    //三方订单id
    private var thirdOrderId = ""
    fun saveMullaThirdOrderId(orderId: String){
        thirdOrderId = orderId
    }

    /**
     * 获取支付状态
     */
    fun loadMullaPaymentStatusAction(isStartCountdown: Boolean = true){
        mvpView?.showMullaLoading()
        val httpUrl = ApiService.getHttpUrl(ApiService.API_PATH.MULLA_API_LOAN_REPAYMENT_STATUS_PATH)
        val params = JSONObject().addCommonParams().addRandomParams()
        params.put("product",mvpView?.loadProductName())
        params.put("token",mvpView?.loadProductToken())
        params.put("thirdOrderId",thirdOrderId)//三方订单id
        HttpHelper.postMullaAction(httpUrl,params,object : IHttpCallback{
            override fun onRequestSuccess(data: JSONObject) {
                mvpView?.hideMullaLoading()
                when(data.optInt("code")) {
                    0 -> {//成功
                        val obj = data.optJSONObject("obj")
                        if (obj != null) {
                            obj.put("isStartCountdown",isStartCountdown)
                            if (obj.has("transactionId")){
                                thirdOrderId = obj.optString("transactionId")
                            }

                            // PROCESSING /SUCCEEDED /FAILED
                            val status = obj.optString("status")
                            when(status){
                                "FAILED" -> {
                                    mvpView?.showMullaPaymentFailed(obj)
                                }
                                "SUCCEEDED" -> {
                                    mvpView?.showMullaPaymentSuccess(obj)
                                }
                                else -> {
                                    mvpView?.showMullaPaymentProcess(obj)
                                }
                            }
                        }
                    }
                    else -> {
                        val msg = data.optString("msg")
                        mvpView?.showMullaToast(msg)
                    }
                }
            }

            override fun onRequestError(error: JSONObject) {
                mvpView?.showMullaToast(error.optString("errorMsg"))
            }

            override fun onRequestFinish() {
                mvpView?.hideMullaLoading()
            }
        })
    }


    /**
     * 订单还款
     * 不减免 normal
     * 积分减免 score
     * 优惠券减免 coupon
     */
    fun loadMullaLoanPayLinkAction(){
        mvpView?.showMullaLoading()
        val httpUrl = ApiService.getHttpUrl(ApiService.API_PATH.MULLA_API_LOAN_REPAYMENT_URL_PATH)
        val params = JSONObject().addCommonParams().addRandomParams().apply {
            put("product",mvpView?.loadProductName())
            put("token",mvpView?.loadProductToken())
            put("mobile",SharedHelper.getMullaSPString(SharedHelper.MULLA_SPKEY.MULLA_MOBILE))
        }
        mvpView?.let { view ->
            if (view.loadCouponId().isNotEmpty()){
                params.put("annulType","coupon")
                params.put("couponId",view.loadCouponId())
            }
        }
        HttpHelper.postMullaAction(httpUrl,params,object : IHttpCallback{
            override fun onRequestSuccess(data: JSONObject) {
                mvpView?.hideMullaLoading()
                when(data.optInt("code")) {
                    0 -> {//成功
                        val obj = data.optJSONObject("obj")
                        if (obj != null){
                            thirdOrderId = obj.optString("thirdOrderId")
                            loadMullaPaymentStatusAction()
                        }
                    }
                    else -> {
                        mvpView?.showMullaToast(data.optString("msg"))
                    }
                }
            }

            override fun onRequestError(error: JSONObject) {
                mvpView?.showMullaToast(error.optString("errorMsg"))
            }

            override fun onRequestFinish() {
            }
        })
    }
}